﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using csla4Skeleton.UI.SL.NghiepVu.TienMat.TM_PhieuThu;
using csla4Skeleton.UI.SL.NghiepVu.Kho.K_PhieuXuatKho;
using csla4Skeleton.UI.SL.Resources.Utility;
using csla4Skeleton.UI.SL.DanhMuc.DM_NhaCungCap;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_PhuongThucThanhToan;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiHoaDon;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiMuaSanPham;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiTienTe;
using csla4Skeleton.UI.SL.DanhMuc.DM_HangHoa;
using csla4Skeleton.UI.SL.DanhMuc.DM_Kho;
using csla4Skeleton.UI.SL.DanhMuc.DM_TaiKhoan;
using csla4Skeleton.UI.SL.DanhMuc.DM_DonViTinh;
using csla4Skeleton.UI.SL.DanhMuc.DM_ThueSuat;
using csla4Skeleton.UI.SL.DanhMuc.DM_TinhChatHangHoa;
using csla4Skeleton.UI.SL.NghiepVu.Kho.K_ListNhapKho;
using csla4Skeleton.UI.SL.NghiepVu.Kho.K_ListXuatKho;
using csla4Skeleton.UI.SL.NghiepVu.Kho.K_TonKho;
using csla4Skeleton.UI.SL.HeThong.HT_ThongTinCongTy;
using Accounting.Library;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;
using ConfigurationNghiepVu;
namespace csla4Skeleton.UI.SL.NghiepVu.BanSanPham.BSP_BanHangHoa
{
    public partial class BSP_BanHangHoa_MainWindow: UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            try
            {
                if (parameters == "")
                    Khoa = null;
                else
                    Khoa = Int32.Parse(parameters);
            }
            catch
            {
                Khoa = null;
            }
        }

        public string Title
        {
            get { return "BanHangHoa"; }
        }

        #endregion

        #region Load Methods
        private System.Nullable<int> Khoa = null;
        private int HangHoaIdCanChinhSua = 0;
        private double GiaVonCanChinhSua = 0;
        private double GiaVonCu = 0;
        int IndexOfBanHangHoaCanChinhSua = 0;
        IList<K_KhoInfo> TotalListCanChinhSua = new List<K_KhoInfo>();
        public delegate void GiaVonChinhSua_EventHandler(object sender, GiaVonChinhSuaEventArgs e);
       
        public BSP_BanHangHoa_MainWindow(int Khoa)          
        {
            this.Khoa = Khoa;
        }

        public BSP_BanHangHoa_MainWindow()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.BanSanPham.BSP_BanHangHoa.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
    
            InitializeComponent();
            ControlBehavior.ControlBehaviors.Navigation(this.BSP_BanHangHoa_GeneralInformation_GroupBox, controls);
            ControlBehavior.ControlBehaviors.Navigation(this.BSP_BanHangHoa_MoreInformation_GroupBox, controls);

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                
                System.Windows.Data.CollectionViewSource BSP_BanHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["BSP_BanHangHoa_ViewModelSource"];
                if (Khoa == null)
                {
                    BSP_BanHangHoa_ViewModel BSP_BanHangHoa_ViewModel = new BSP_BanHangHoa_ViewModel();
                    BSP_BanHangHoa_ViewModel.ChildList_AddedNew += new BSP_BanHangHoa_ViewModel.ChildList_AddedNew_EventHandler(BSP_BanHangHoa_ViewModel_ChildList_AddedNew);
                    BSP_BanHangHoa_ViewModel.stateChanged += new BSP_BanHangHoa_ViewModel.DKNV_EventHandler(BSP_BanHangHoa_StateChanged);
                    BSP_BanHangHoa_ViewModel.TCHangHoaChanged += new EventHandler(BSP_BanHangHoa_TinhChatHangHoaChanged);
                    BSP_BanHangHoa_ViewModel.hangHoaIdChanged += new BSP_BanHangHoa_ViewModel.HangHoaIdChanged_EventHandler(BSP_BanHangHoa_HangHoaIdChanged);
                    BSP_BanHangHoa_ViewModel.TKCoIdChanged += new BSP_BanHangHoa_ViewModel.TKCoIdChanged_EventHandler(BSP_BanHangHoa_TKCoChanged);
                    BSP_BanHangHoa_ViewModel.LoaiTienTeChanged += new EventHandler(BSP_BanHangHoa_LoaiTienTeChanged);
                    BSP_BanHangHoa_ViewModel.EnableButtonLapPhieu += new EventHandler(BSP_BanHangHoa_EnableButtonLapPhieu);
                    BSP_BanHangHoa_ViewModel.ThueSuatIdChanged += new BSP_BanHangHoa_ViewModel.ThueSuatIdChanged_EventHandler(BSP_BanHangHoa_ThueSuatIdChanged);
                    BSP_BanHangHoa_ViewModel.SoLuongChanged += new BSP_BanHangHoa_ViewModel.SoLuongChanged_EventHandler(BSP_BanHangHoa_SoLuongChanged);
                    BSP_BanHangHoa_ViewModel.LoadCompleted += new EventHandler(OnBanHangHoaLoadCompleteted);
                    BSP_BanHangHoa_ViewModel.NgayChungTuChanged += new EventHandler(OnNgayChungTuChanged);
                    BSP_BanHangHoa_ViewModel.Saved+=new EventHandler(BSP_BanHangHoa_ViewModel_Saved);
                    BSP_BanHangHoa_CollectionViewSource.Source = new List<object> { BSP_BanHangHoa_ViewModel };

                    
                }
                else
                {
                    BSP_BanHangHoa_ViewModel BSP_BanHangHoa_ViewModel = new BSP_BanHangHoa_ViewModel((int)Khoa);
                    BSP_BanHangHoa_ViewModel.ChildList_AddedNew += new BSP_BanHangHoa_ViewModel.ChildList_AddedNew_EventHandler(BSP_BanHangHoa_ViewModel_ChildList_AddedNew);
                    BSP_BanHangHoa_ViewModel.stateChanged += new BSP_BanHangHoa_ViewModel.DKNV_EventHandler(BSP_BanHangHoa_StateChanged);
                    BSP_BanHangHoa_ViewModel.TCHangHoaChanged += new EventHandler(BSP_BanHangHoa_TinhChatHangHoaChanged);
                    BSP_BanHangHoa_ViewModel.hangHoaIdChanged += new BSP_BanHangHoa_ViewModel.HangHoaIdChanged_EventHandler(BSP_BanHangHoa_HangHoaIdChanged);
                    BSP_BanHangHoa_ViewModel.TKCoIdChanged += new BSP_BanHangHoa_ViewModel.TKCoIdChanged_EventHandler(BSP_BanHangHoa_TKCoChanged);
                    BSP_BanHangHoa_ViewModel.LoaiTienTeChanged += new EventHandler(BSP_BanHangHoa_LoaiTienTeChanged);
                    BSP_BanHangHoa_ViewModel.LoadCompleted += new EventHandler(OnBanHangHoaLoadCompleteted);
                    BSP_BanHangHoa_ViewModel.EnableButtonLapPhieu += new EventHandler(BSP_BanHangHoa_EnableButtonLapPhieu);
                    BSP_BanHangHoa_ViewModel.SoLuongChanged += new BSP_BanHangHoa_ViewModel.SoLuongChanged_EventHandler(BSP_BanHangHoa_SoLuongChanged);
                    BSP_BanHangHoa_ViewModel.ThueSuatIdChanged += new BSP_BanHangHoa_ViewModel.ThueSuatIdChanged_EventHandler(BSP_BanHangHoa_ThueSuatIdChanged);
                    BSP_BanHangHoa_ViewModel.NgayChungTuChanged += new EventHandler(OnNgayChungTuChanged);
                    BSP_BanHangHoa_ViewModel.Saved += new EventHandler(BSP_BanHangHoa_ViewModel_Saved);
                    BSP_BanHangHoa_CollectionViewSource.Source = new List<object> { BSP_BanHangHoa_ViewModel };

                    
                }

                
                System.Windows.Data.CollectionViewSource dm_NhaCungCap_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
                DM_NhaCungCap_ListViewModel dm_NhaCungCap_ListViewModel = new DM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_KhachHang);
                dm_NhaCungCap_CollectionViewSource.Source = new List<object> { dm_NhaCungCap_ListViewModel };

                
                System.Windows.Data.CollectionViewSource ht_ThongTinCongTy_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_ThongTinCongTy_ListViewModelSource"];
                HT_ThongTinCongTy_ListViewModel ht_ThongTinCongTy_ListViewModel = new HT_ThongTinCongTy_ListViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
                ht_ThongTinCongTy_CollectionViewSource.Source = new List<object> { ht_ThongTinCongTy_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_PhuongThucThanhToan_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_PhuongThucThanhToan_ListViewModelSource"];
                DM_PhuongThucThanhToan_ListViewModel dm_PhuongThucThanhToan_ListViewModel = new DM_PhuongThucThanhToan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_PhuongThucThanhToan_CollectionViewSource.Source = new List<object> { dm_PhuongThucThanhToan_ListViewModel };

                System.Windows.Data.CollectionViewSource k_ListNhapKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_ListNhapKho_ListViewModelSource"];
                K_ListNhapKho_ListViewModel k_ListNhapKho_ListViewModel = new K_ListNhapKho_ListViewModel(DateTime.Now);
                k_ListNhapKho_CollectionViewSource.Source = new List<object> { k_ListNhapKho_ListViewModel };

                System.Windows.Data.CollectionViewSource k_ListXuatKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_ListXuatKho_ListViewModelSource"];
                K_ListXuatKho_ListViewModel k_ListXuatKho_ListViewModel = new K_ListXuatKho_ListViewModel(DateTime.Now);
                k_ListXuatKho_CollectionViewSource.Source = new List<object> { k_ListXuatKho_ListViewModel };

                System.Windows.Data.CollectionViewSource k_TonKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_TonKho_ViewModelSource"];
                K_TonKho_ViewModel k_TonKho_ViewModel = new K_TonKho_ViewModel(DateTime.Now.AddMonths(-1));
                k_TonKho_CollectionViewSource.Source = new List<object> { k_TonKho_ViewModel };


                System.Windows.Data.CollectionViewSource dm_LoaiHoaDon_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiHoaDon_ListViewModelSource"];
                DM_LoaiHoaDon_ListViewModel dm_LoaiHoaDon_ListViewModel = new DM_LoaiHoaDon_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiHoaDon_CollectionViewSource.Source = new List<object> { dm_LoaiHoaDon_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiBanSanPham_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiMuaSanPham_ListViewModelSource"];
                DM_LoaiMuaSanPham_ListViewModel dm_LoaiBanSanPham_ListViewModel = new DM_LoaiMuaSanPham_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiBanSanPham_CollectionViewSource.Source = new List<object> { dm_LoaiBanSanPham_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiTienTe_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
                DM_LoaiTienTe_ListViewModel dm_LoaiTienTe_ListViewModel = new DM_LoaiTienTe_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiTienTe_CollectionViewSource.Source = new List<object> { dm_LoaiTienTe_ListViewModel };


                System.Windows.Data.CollectionViewSource dm_Kho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_Kho_ListViewModelSource"];
                DM_Kho_ListViewModel dm_Kho_ListViewModel = new DM_Kho_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_Kho_CollectionViewSource.Source = new List<object> { dm_Kho_ListViewModel };

               


                System.Windows.Data.CollectionViewSource dm_DonViTinh_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DonViTinh_ListViewModelSource"];
                DM_DonViTinh_ListViewModel dm_DonViTinh_ListViewModel = new DM_DonViTinh_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_DonViTinh_CollectionViewSource.Source = new List<object> { dm_DonViTinh_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
                DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = new DM_ThueSuat_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_ThueSuat_CollectionViewSource.Source = new List<object> { dm_ThueSuat_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TinhChatHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TinhChatHangHoa_ListViewModelSource"];
                DM_TinhChatHangHoa_ListViewModel dm_TinhChatHangHoa_ListViewModel = new DM_TinhChatHangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TinhChatHangHoa_CollectionViewSource.Source = new List<object> { dm_TinhChatHangHoa_ListViewModel };

                DoFilterOnDefaultDKNV1();

                System.Windows.Data.CollectionViewSource dm_TaiKhoanKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanKho_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanKho_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
             
                dm_TaiKhoanKho_CollectionViewSource.Source = new List<object> { dm_TaiKhoanKho_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TkChieuKhauId_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TkChieuKhauId_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TkChieuKhauId_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);

                dm_TkChieuKhauId_CollectionViewSource.Source = new List<object> { dm_TkChieuKhauId_ListViewModel };
               
                DoFilterOnDefaultDM_SanPham();

                this.BSP_BanHangHoa_BanHangHoaChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.BSP_BanHangHoa_BanHangHoaChiTietGridView);
                this.BSP_BanHangHoa_ThueChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.BSP_BanHangHoa_ThueChiTietGridView);
                this.BSP_BanHangHoa_PhuPhiGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.BSP_BanHangHoa_PhuPhiGridView);
                this.BSP_BanHangHoa_GiaVonGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.BSP_BanHangHoa_GiaVonGridView);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanHangHoa_BanHangHoaChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanHangHoa_ThueChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanHangHoa_PhuPhiGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanHangHoa_GiaVonGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanHangHoa_BanHangHoaChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanHangHoa_ThueChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanHangHoa_PhuPhiGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanHangHoa_GiaVonGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                
            }
        }

        #endregion
       

        #region Event Handle
        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                BSP_BanHangHoa_ViewModel vm = GetViewModel();
                if (e.Key == Key.S)
                {
                    if (vm.CanSave)
                    {
                        vm.Save(sender, new Csla.Xaml.ExecuteEventArgs());
                    }
                }
                else if (e.Key == Key.F6)
                {
                    AddItems();
                }
                else if (e.Key == Key.D1)
                {
                    this.BSP_BanHangHoa_BanHangHoaChiTietTab.IsSelected = true;
                }
                else if (e.Key == Key.D2)
                {
                    this.BSP_BanHangHoa_ThueChiTietTab.IsSelected = true;
                }
                else if (e.Key == Key.D3)
                {
                    this.BSP_BanHangHoa_PhuPhiTab.IsSelected = true;
                }
                else if (e.Key == Key.D4)
                {
                    this.BSP_BanHangHoa_GiaVonTab.IsSelected = true;
                }
               
            }
            e.Handled = true;
        }

        void AddItemButton_Click(object sender, RoutedEventArgs e)
        {

            AddItems();
        }

        void DeleteItemButton_Click(object sender, RoutedEventArgs e)
        {
            DeleteItems();
          
        }


        public void AddItems()
        {
            if (this.BSP_BanHangHoa_BanHangHoaChiTietTab.IsSelected)
            {
                AddBanHangHoaDetailsItem();
            }
        }
        private RadGridView GetActiveGridView()
        {
            if (this.BSP_BanHangHoa_BanHangHoaChiTietTab.IsSelected)
            {
                return BSP_BanHangHoa_BanHangHoaChiTietGridView;
            }
            else if (this.BSP_BanHangHoa_ThueChiTietTab.IsSelected)
            {
                return BSP_BanHangHoa_ThueChiTietGridView;
            }
            else if (this.BSP_BanHangHoa_PhuPhiTab.IsSelected)
            {
                return BSP_BanHangHoa_PhuPhiGridView;
            }
            else if (this.BSP_BanHangHoa_GiaVonTab.IsSelected)
            {
                return BSP_BanHangHoa_GiaVonGridView;
            }
          
            return null;
        }



        void DeleteItems()
        {
           if (this.BSP_BanHangHoa_BanHangHoaChiTietTab.IsSelected)
            {
                BSP_BanHangHoa_ChiTietChild ChiTietChild = (BSP_BanHangHoa_ChiTietChild)(this.BSP_BanHangHoa_BanHangHoaChiTietGridView.SelectedItem);
                IList<BSP_BanHangHoa_ChiTietChild> list = GetViewModel().Model.BSP_BanHangHoa_ChiTiets;
                int index = list.IndexOf(ChiTietChild);
                
                DeleteBanHangHoaDetailsItem(index);
            }
           
        }
           
        void GridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            var grid = e.OwnerGridViewItemsControl;
            if (grid.Items.Count > 0)
            {
                GridViewUtil.FocusRow((RadGridView)grid, grid.Items.Count - 1);
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
            }
            
        }
              
        private void SaveAndNewButton_Click(object sender, RoutedEventArgs e)
        {
           BSP_BanHangHoa_ViewModel vm = GetViewModel();
            vm.SaveAndNew();
        }

        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddBanHangHoaDetailsItem();
                }
            }
            else
            {
                ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, controls);
            }
        }

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddBanHangHoaDetailsItem();
                }
            }
            else
            {
                ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
            }
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddBanHangHoaDetailsItem();
                }
            }
            else
            {
                ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
            }
        }


        private void BookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().GhiSo();
        }

        private void DeleteBookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().BoGhi();
        }

        private void LapPhieuThuItemButton_Click(object sender, RoutedEventArgs e)
        {

            if (this.GetViewModel().Model.Khoa != 0 && this.GetViewModel().Model.LoaiChungTuId != null)
            {
                System.Windows.Data.CollectionViewSource tm_PhieuThu_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TM_PhieuThu_ViewModelSource"];
                TM_PhieuThu_ViewModel tm_PhieuThu_ViewModel = new TM_PhieuThu_ViewModel(this.GetViewModel().Model.Khoa, this.GetViewModel().Model.LoaiChungTuId);
                tm_PhieuThu_ViewModel.LoadCompleted += new EventHandler(PhieuThu_LoadCompleted);
                tm_PhieuThu_CollectionViewSource.Source = new List<object> { tm_PhieuThu_ViewModel };
            }
            else
            {
                MessageBoxResult result = MessageBox.Show("Không thể thực hiện chức năng này vi chứng từ không lưu được?", "Thông báo", MessageBoxButton.OK);
            }

           
        }

        private void PhieuThu_LoadCompleted(object sender, EventArgs e)
        {

            //System.Windows.Data.CollectionViewSource tm_PhieuThu_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TM_PhieuThu_ViewModelSource"];
            //TM_PhieuThu_ViewModel tm_PhieuThu_ViewModel = ((List<object>)tm_PhieuThu_CollectionViewSource.Source)[0] as TM_PhieuThu_ViewModel;

            //if (tm_PhieuThu_ViewModel.Model.Khoa == 0)
            //{
            //    FormNguoiNop formNguoiNop = new FormNguoiNop(this.GetViewModel(), NghiepVuType.BanHangHoa);
            //    formNguoiNop.Show();
            //    this.LapPhieuThuItemButton.Content = "Xem phiếu thu";
            //}
            //else
            //{

            //    TM_PhieuThu_ChildWindow tm_PhieuThu_ChildWindow = new TM_PhieuThu_ChildWindow(tm_PhieuThu_ViewModel, this.GetViewModel(), NghiepVuType.BanHangHoa);
            //    tm_PhieuThu_ChildWindow.Show();
            //    tm_PhieuThu_ChildWindow.Visibility = Visibility.Collapsed;
            //    MessageBoxResult result = MessageBox.Show("Phiếu thu đã tồn tại, bạn có muốn xem phiếu thu không ?", "Xem phiếu thu", MessageBoxButton.OKCancel);

            //    if (result == MessageBoxResult.OK)
            //    {
            //        tm_PhieuThu_ChildWindow.Visibility = Visibility.Visible;
            //        tm_PhieuThu_ChildWindow.CountSave++;

            //    }
            //    else if (result == MessageBoxResult.Cancel)
            //    {
            //        tm_PhieuThu_ChildWindow.Close();
            //    }
            //}
        }

        private void LapPhieuXuatItemButton_Click(object sender, RoutedEventArgs e)
        {            
            //if (this.GetViewModel().Model.Khoa != 0 && this.GetViewModel().Model.LoaiChungTuId != null)
            //{
            //    System.Windows.Data.CollectionViewSource k_PhieuXuatKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_PhieuXuatKho_ViewModelSource"];
            //    K_PhieuXuatKho_ViewModel k_PhieuXuatKho_ViewModel = new K_PhieuXuatKho_ViewModel(this.GetViewModel().Model.Khoa, this.GetViewModel().Model.LoaiChungTuId);
            //    k_PhieuXuatKho_ViewModel.LoadCompleted += new EventHandler(PhieuXuat_LoadCompleted);
            //    k_PhieuXuatKho_CollectionViewSource.Source = new List<object> { k_PhieuXuatKho_ViewModel };
            //}
            //else
            //{
            //    MessageBoxResult result = MessageBox.Show("Không thể thực hiện chức năng này vi chứng từ không lưu được?", "Thông báo", MessageBoxButton.OK);
            //}
        }

       
        private void PhieuXuat_LoadCompleted(object sender, EventArgs e)
        {

            System.Windows.Data.CollectionViewSource k_PhieuXuatKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_PhieuXuatKho_ViewModelSource"];
            K_PhieuXuatKho_ViewModel k_PhieuXuatKho_ViewModel = ((List<object>)k_PhieuXuatKho_CollectionViewSource.Source)[0] as K_PhieuXuatKho_ViewModel;

            //if (k_PhieuXuatKho_ViewModel.Model.Khoa == 0)
            //{
            //    FormNguoiNhan formNguoiNhan = new FormNguoiNhan(this.GetViewModel(), NghiepVuType.BanHangHoa);
            //    formNguoiNhan.Show();
            //    this.LapPhieuXuatItemButton.Content = "Xem phiếu xuất";
            //}
            //else
            //{

            //    K_PhieuXuatKho_ChildWindow k_PhieuXuatKho_ChildWindow = new K_PhieuXuatKho_ChildWindow(k_PhieuXuatKho_ViewModel, this.GetViewModel(), NghiepVuType.BanHangHoa);
            //    k_PhieuXuatKho_ChildWindow.Show();
            //    k_PhieuXuatKho_ChildWindow.Visibility = Visibility.Collapsed;
            //    MessageBoxResult result = MessageBox.Show("Phiếu xuất đã tồn tại, bạn có muốn xem phiếu xuất không ?", "Xem phiếu xuất", MessageBoxButton.OKCancel);

            //    if (result == MessageBoxResult.OK)
            //    {
            //        k_PhieuXuatKho_ChildWindow.Visibility = Visibility.Visible;
            //        k_PhieuXuatKho_ChildWindow.CountSave++;

            //    }
            //    else if (result == MessageBoxResult.Cancel)
            //    {
            //        k_PhieuXuatKho_ChildWindow.Close();
            //    }
            //}
        }
        void OnTabKeyPress(object sender, EventArgs e)
        {
            this.tabControl.SelectedItem = tabControl.Items[0];
            this.AddBanHangHoaDetailsItem();
        }

        void OnReachLastColumn(object sender, ReachLastColumnArgs e)
        {

            List<TabItem> AvailableTabItem = new List<TabItem>();
            int indexCurrentTab = 0;
            foreach (TabItem item in this.tabControl.Items)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    AvailableTabItem.Add(item);
                    if (item.GetHashCode() == tabControl.SelectedItem.GetHashCode())
                    {
                        indexCurrentTab = AvailableTabItem.IndexOf(item);
                    }

                }
            }


            TabItem[] list = AvailableTabItem.ToArray();
            if (indexCurrentTab == (AvailableTabItem.Count - 1))
            {
                this.tabControl.SelectedItem = list[0];
                FocusAfterChangeTab(this.BSP_BanHangHoa_BanHangHoaChiTietGridView, e.Index);

            }
            else
            {
                this.tabControl.SelectedItem = list[indexCurrentTab + 1];
                if (list[indexCurrentTab + 1].Name.Equals("BSP_BanHangHoa_PhuPhiTab"))
                {
                    FocusAfterChangeTab(this.BSP_BanHangHoa_PhuPhiGridView, e.Index);
                }
                else if (list[indexCurrentTab + 1].Name.Equals("BSP_BanHangHoa_ThueChiTietTab"))
                {
                    FocusAfterChangeTab(this.BSP_BanHangHoa_ThueChiTietGridView, e.Index);

                }
                else if (list[indexCurrentTab + 1].Name.Equals("BSP_BanHangHoa_GiaVonTab"))
                {
                    FocusAfterChangeTab(this.BSP_BanHangHoa_GiaVonGridView, e.Index);
                }

            }

        }

        private void FocusAfterChangeTab(GridViewDataControl grid, int index)
        {
            
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[index], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
           
        }
        private void BSP_BanHangHoa_GeneralInformation_NhaCungCapId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_NhaCungCap_ChildWindow dm_NhaCungCap_ChildWindow = new DM_NhaCungCap_ChildWindow();
            dm_NhaCungCap_ChildWindow.Show();
            dm_NhaCungCap_ChildWindow.SaveClicked += new EventHandler(dm_NhaCungCap_ViewModel_Saved);
        }

        private void BSP_BanHangHoa_MoreInformation_PhuongThucThanhToanId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_PhuongThucThanhToan_ChildWindow dm_PhuongThucThanhToan_ChildWindow = new DM_PhuongThucThanhToan_ChildWindow();
            dm_PhuongThucThanhToan_ChildWindow.Show();
            dm_PhuongThucThanhToan_ChildWindow.SaveClicked += new EventHandler(dm_PhuongThucThanhToan_ViewModel_Saved);
        }

        private void BSP_BanHangHoa_MoreInformation_LoaiTienId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_LoaiTienTe_ChildWindow dm_LoaiTienTe_ChildWindow = new DM_LoaiTienTe_ChildWindow();
            dm_LoaiTienTe_ChildWindow.Show();
            dm_LoaiTienTe_ChildWindow.SaveClicked += new EventHandler(dm_LoaiTienTe_ViewModel_Saved);
        }

        private void BSP_BanHangHoa_MoreInformation_LoaiBanSanPhamId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_LoaiMuaSanPham_ChildWindow dm_LoaiBanSanPham_ChildWindow = new DM_LoaiMuaSanPham_ChildWindow();
            dm_LoaiBanSanPham_ChildWindow.Show();
            dm_LoaiBanSanPham_ChildWindow.SaveClicked += new EventHandler(dm_LoaiBanSanPham_ViewModel_Saved);
        }
       
        #endregion

        #region View Model Related Methods
        private BSP_BanHangHoa_ViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource BSP_BanHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["BSP_BanHangHoa_ViewModelSource"];
            return ((List<object>)BSP_BanHangHoa_CollectionViewSource.Source)[0] as BSP_BanHangHoa_ViewModel;
        }

        void dm_NhaCungCap_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_NhaCungCap_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
            DM_NhaCungCap_ListViewModel vm = ((List<object>)dm_NhaCungCap_ListViewSource.Source)[0] as DM_NhaCungCap_ListViewModel;
            vm.GetDM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_KhachHang);
        }

        void dm_LoaiTienTe_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_LoaiTienTe_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
            DM_LoaiTienTe_ListViewModel vm = ((List<object>)dm_LoaiTienTe_ListViewSource.Source)[0] as DM_LoaiTienTe_ListViewModel;
            vm.GetDM_LoaiTienTe_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_LoaiBanSanPham_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_LoaiBanSanPham_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiMuaSanPham_ListViewModelSource"];
            DM_LoaiMuaSanPham_ListViewModel vm = ((List<object>)dm_LoaiBanSanPham_ListViewSource.Source)[0] as DM_LoaiMuaSanPham_ListViewModel;
            vm.GetDM_LoaiMuaSanPham_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_PhuongThucThanhToan_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_PhuongThucThanhToan_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_PhuongThucThanhToan_ListViewModelSource"];
            DM_PhuongThucThanhToan_ListViewModel vm = ((List<object>)dm_PhuongThucThanhToan_ListViewSource.Source)[0] as DM_PhuongThucThanhToan_ListViewModel;
            vm.GetDM_PhuongThucThanhToan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        private void OnNgayChungTuChanged(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource k_ListXuatKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_ListXuatKho_ListViewModelSource"];
            K_ListXuatKho_ListViewModel k_ListXuatKho_ListViewModel = new K_ListXuatKho_ListViewModel((DateTime)this.GetViewModel().Model.NgayChungTu);
            k_ListXuatKho_CollectionViewSource.Source = new List<object> { k_ListXuatKho_ListViewModel };

            System.Windows.Data.CollectionViewSource k_ListNhapKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_ListNhapKho_ListViewModelSource"];
            K_ListNhapKho_ListViewModel k_ListNhapKho_ListViewModel = new K_ListNhapKho_ListViewModel((DateTime)this.GetViewModel().Model.NgayChungTu);
            k_ListNhapKho_CollectionViewSource.Source = new List<object> { k_ListNhapKho_ListViewModel };

            System.Windows.Data.CollectionViewSource k_TonKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_TonKho_ViewModelSource"];
            K_TonKho_ViewModel k_TonKho_ViewModel = new K_TonKho_ViewModel((DateTime)this.GetViewModel().Model.NgayChungTu.Value.AddMonths(-1));
            k_TonKho_CollectionViewSource.Source = new List<object> { k_TonKho_ViewModel };

           
        }
        private void OnBanHangHoaLoadCompleteted(object sender, EventArgs e)
        {
            if (this.GetViewModel().Model.DaThanhToan == true && this.GetViewModel().Model.Khoa != 0)
            {
                this.LapPhieuThuItemButton.IsEnabled = true;

            }
            else
            {
                this.LapPhieuThuItemButton.IsEnabled = false;

            }

            if (this.GetViewModel().Model.Khoa != 0)
            {
                this.LapPhieuXuatItemButton.IsEnabled = true;
            }
            else
            {
                this.LapPhieuXuatItemButton.IsEnabled = false;
            }
        }
        public void DoFilterOnDefaultDM_SanPham()
        {
           
                System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
                DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = new DM_HangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_HangHoa_CollectionViewSource.Source = new List<object> { dm_HangHoa_ListViewModel };
           
        }
        void BSP_BanHangHoa_TKCoChanged(object sender, TKCoIdChangedEventArgs e)
        {
            if (this.GetViewModel().Model.LoaiBanSanPhamId == 3)
            {
                BSP_BanHangHoa_ChiTietChild ChiTietChild = GetViewModel().Model.BSP_BanHangHoa_ChiTiets[e.Index];
                BSP_BanHangHoa_ThueChild ThueChild = GetViewModel().Model.BSP_BanHangHoa_Thues[e.Index];
                ThueChild.TKCoId = ChiTietChild.TKCoId;
                
            }
            else if (this.GetViewModel().Model.LoaiHDId == 1 || this.GetViewModel().Model.LoaiHDId == null)
            {

                BSP_BanHangHoa_ChiTietChild ChiTietChild = GetViewModel().Model.BSP_BanHangHoa_ChiTiets[e.Index];
                BSP_BanHangHoa_ThueChild ThueChild = GetViewModel().Model.BSP_BanHangHoa_Thues[e.Index];
                ThueChild.TKCoId = ChiTietChild.TKCoId;     
            }

        }

        void BSP_BanHangHoa_SoLuongChanged(object sender, SoLuongChangedEventArgs e)
        {
            System.Windows.Data.CollectionViewSource ht_ThongTinCongTy_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_ThongTinCongTy_ListViewModelSource"];
            HT_ThongTinCongTy_ListViewModel ht_ThongTinCongTy_ListViewModel = ((List<object>)ht_ThongTinCongTy_CollectionViewSource.Source)[0] as HT_ThongTinCongTy_ListViewModel;

            if (ht_ThongTinCongTy_ListViewModel.Model.getListOfInfo().Count == 0)
            {
                DoTinhGiaVon(e, "NTXT");
            }
            else
            {
                if (ht_ThongTinCongTy_ListViewModel.Model.getListOfInfo()[0].PhuongPhapTinhGiaVonId == 3)
                {
                    DoTinhGiaVonBinhQuanTucThi(e);
                }

                else if (ht_ThongTinCongTy_ListViewModel.Model.getListOfInfo()[0].PhuongPhapTinhGiaVonId == 1)
                {
                    DoTinhGiaVon(e, "NTXT");
                }
                else if (ht_ThongTinCongTy_ListViewModel.Model.getListOfInfo()[0].PhuongPhapTinhGiaVonId == 2)
                {
                    DoTinhGiaVon(e, "NSXT");
                }
            }
        }
      
        //int IndexOfRow = 0;
        public void DoTinhGiaVonBinhQuanTucThi(SoLuongChangedEventArgs e)
        {
            //IList<K_ListNhapKhoInfo> listNhapKho = null;
            //IList<K_ListXuatKhoInfo> listXuatKho = null;
                    
            //System.Windows.Data.CollectionViewSource k_ListNhapKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_ListNhapKho_ListViewModelSource"];
            //K_ListNhapKho_ListViewModel k_ListNhapKho_ListViewModel = ((List<object>)k_ListNhapKho_CollectionViewSource.Source)[0] as K_ListNhapKho_ListViewModel;
            //if (k_ListNhapKho_ListViewModel.Model != null)
            //{
            //    listNhapKho = k_ListNhapKho_ListViewModel.Model.getListOfInfo();
            //}

            //System.Windows.Data.CollectionViewSource k_ListXuatKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_ListXuatKho_ListViewModelSource"];
            //K_ListXuatKho_ListViewModel k_ListXuatKho_ListViewModel = ((List<object>)k_ListXuatKho_CollectionViewSource.Source)[0] as K_ListXuatKho_ListViewModel;
            //if (k_ListXuatKho_ListViewModel.Model != null)
            //{
            //    listXuatKho = k_ListXuatKho_ListViewModel.Model.getListOfInfo();
            //}

            //System.Windows.Data.CollectionViewSource k_TonKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_TonKho_ViewModelSource"];
            //K_TonKho_ViewModel k_TonKho_ViewModel = ((List<object>)k_TonKho_CollectionViewSource.Source)[0] as K_TonKho_ViewModel;

           

            //List<K_KhoInfo> ListTongHop= new List<K_KhoInfo>();
            //IList<BSP_BanHangHoa_ChiTietChild> BanHangHoaChiTietList = this.GetViewModel().Model.BSP_BanHangHoa_ChiTiets;
                    
            //if (listXuatKho != null)
            //{
            //    foreach (K_ListXuatKhoInfo ListKhoInfo in listXuatKho)
            //    {
            //        if (ListKhoInfo.HangHoaId == BanHangHoaChiTietList[e.Index].HangHoaId && ListKhoInfo.SoLuong != null)
            //        {
            //            K_KhoInfo khoInfo = new K_KhoInfo(ListKhoInfo.HangHoaId, -1*ListKhoInfo.SoLuong, ListKhoInfo.DonGia, ListKhoInfo.NgayChungTu, ListKhoInfo.ChungTuThayTheId);
            //            ListTongHop.Add(khoInfo);
            //        }
            //    }
            //}
                                 
            //if (listNhapKho != null)
            //{
            //    foreach (K_ListNhapKhoInfo ListKhoInfo in listNhapKho)
            //    {
            //        if (ListKhoInfo.HangHoaId == BanHangHoaChiTietList[e.Index].HangHoaId && ListKhoInfo.SoLuong != null)
            //        {
            //            K_KhoInfo khoInfo = new K_KhoInfo(ListKhoInfo.HangHoaId, ListKhoInfo.SoLuong, ListKhoInfo.DonGia, ListKhoInfo.NgayChungTu);
            //            ListTongHop.Add(khoInfo);
            //        }
            //    }
            //}
            //ListTongHop = K_KhoInfo.NhapTruocXuatTruocSort(ListTongHop);

            //K_KhoInfo TonKhoInfo = null;
            //if (k_TonKho_ViewModel.Model != null && k_TonKho_ViewModel.Model.Khoa != 0)
            //{
            //    IList<K_TonKho_ChiTietChild> list = k_TonKho_ViewModel.Model.K_TonKho_ChiTiets;
            //    foreach (K_TonKho_ChiTietChild item in list)
            //    {
            //        if(item.HangHoaId == this.GetViewModel().Model.BSP_BanHangHoa_ChiTiets[e.Index].HangHoaId)
            //        {
            //            TonKhoInfo = new K_KhoInfo(item.HangHoaId, item.SoLuong, item.DonGia,this.GetViewModel().Model.NgayChungTu.Value.AddMonths(-1));
            //        }
            //    }
            //}
            
            //if(TonKhoInfo != null)
            //{
            //    ListTongHop.Insert(0,TonKhoInfo);
            //}
  
            //double SoLuongTaiThoiDiemDangXet = 0;
            //double DonGiaTaiThoiDiemDangXet = 0;
 
            //K_KhoInfo BanHangHoaHienTai = null;
            //foreach (K_KhoInfo item in ListTongHop)
            //{
            //    if (item.SoLuong < 0)
            //    {
            //        if (this.GetViewModel().Model.Khoa != 0 && item.ChungTuThayTheId == this.GetViewModel().Model.Khoa)
            //        {
            //            BanHangHoaHienTai = item;
            //            break;
                        
            //        }
            //        else
            //        {
            //            SoLuongTaiThoiDiemDangXet -= (-1 * item.SoLuong.Value);
            //        }
                         
            //    }
            //    else
            //    {
            //        DonGiaTaiThoiDiemDangXet = (DonGiaTaiThoiDiemDangXet * SoLuongTaiThoiDiemDangXet + item.SoLuong.Value * item.DonGia.Value) / (item.SoLuong.Value + SoLuongTaiThoiDiemDangXet);
            //        SoLuongTaiThoiDiemDangXet += item.SoLuong.Value;
            //    }
            //}
            //if (BanHangHoaChiTietList[e.Index].SoLuong.Value > SoLuongTaiThoiDiemDangXet)
            //{
            //    MessageBoxResult result = MessageBox.Show("Bạn đã nhập số lượng lớn hơn số lượng trong kho. Số lượng còn trong kho là : "+ SoLuongTaiThoiDiemDangXet, "Thông tin tồn kho", MessageBoxButton.OK);
            //    this.GetViewModel().Model.BSP_BanHangHoa_ChiTiets[e.Index].SoLuong = 0;
            //    this.GetViewModel().Model.BSP_BanHangHoa_GiaVons[e.Index].DonGiaVon = 0;

            //}
            //else
            //{
            //    this.GetViewModel().Model.BSP_BanHangHoa_GiaVons[e.Index].DonGiaVonQĐ = DonGiaTaiThoiDiemDangXet;
            //    this.GetViewModel().Model.BSP_BanHangHoa_GiaVons[e.Index].DonGiaVon = DonGiaTaiThoiDiemDangXet / this.GetViewModel().Model.TyGia;
            //    SoLuongTaiThoiDiemDangXet -= BanHangHoaChiTietList[e.Index].SoLuong.Value;
            //    List<K_KhoInfo> ListBanHangHoaCanChinhSua = new List<K_KhoInfo>();
            //    int IndexOfBanHangHoaHienTai = ListTongHop.IndexOf(BanHangHoaHienTai);
            //    for(int i = IndexOfBanHangHoaHienTai + 1 ; i < ListTongHop.Count; i++)
            //    {
            //        if (ListTongHop[i].SoLuong < 0)
            //        {
            //            SoLuongTaiThoiDiemDangXet -= (-1 * ListTongHop[i].SoLuong.Value);
            //            if (ListTongHop[i].ChungTuThayTheId != null && ListTongHop[i].ChungTuThayTheId != 0)
            //            {
            //                ListTongHop[i].DonGia = DonGiaTaiThoiDiemDangXet;
            //                ListBanHangHoaCanChinhSua.Add(ListTongHop[i]);
            //            }
            //        }
            //        else
            //        {
            //            DonGiaTaiThoiDiemDangXet = (DonGiaTaiThoiDiemDangXet * SoLuongTaiThoiDiemDangXet + ListTongHop[i].SoLuong.Value * ListTongHop[i].DonGia.Value) / (ListTongHop[i].SoLuong.Value + SoLuongTaiThoiDiemDangXet);
            //            SoLuongTaiThoiDiemDangXet += ListTongHop[i].SoLuong.Value;
            //        }
            //    }

            //    int CountOfList = TotalListCanChinhSua.Count;
            //    for (int i = CountOfList - 1; i >= 0; i--)
            //    {
            //        if (TotalListCanChinhSua[i].HangHoaId == TotalListCanChinhSua[e.Index].HangHoaId)
            //        {
            //            TotalListCanChinhSua.Remove(TotalListCanChinhSua[i]);
            //        }
            //    }

            //    foreach (K_KhoInfo item in ListBanHangHoaCanChinhSua)
            //    {
            //        if (item.ChungTuThayTheId != null && item.ChungTuThayTheId != 0)
            //        {

            //            K_KhoInfo newKhoInfo = new K_KhoInfo(item.ChungTuThayTheId, item.HangHoaId, item.DonGia);
            //            TotalListCanChinhSua.Add(newKhoInfo);

            //        }
            //    }

            //}            
        }

        public void DoTinhGiaVon(SoLuongChangedEventArgs e, string mode)
        {
            //IList<K_ListNhapKhoInfo> listNhapKho = null;
            //IList<K_ListXuatKhoInfo> listXuatKho = null;

            //System.Windows.Data.CollectionViewSource k_ListNhapKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_ListNhapKho_ListViewModelSource"];
            //K_ListNhapKho_ListViewModel k_ListNhapKho_ListViewModel = ((List<object>)k_ListNhapKho_CollectionViewSource.Source)[0] as K_ListNhapKho_ListViewModel;
            //if (k_ListNhapKho_ListViewModel.Model != null)
            //{
            //    listNhapKho = k_ListNhapKho_ListViewModel.Model.getListOfInfo();
            //}

            //System.Windows.Data.CollectionViewSource k_ListXuatKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_ListXuatKho_ListViewModelSource"];
            //K_ListXuatKho_ListViewModel k_ListXuatKho_ListViewModel = ((List<object>)k_ListXuatKho_CollectionViewSource.Source)[0] as K_ListXuatKho_ListViewModel;
            //if (k_ListXuatKho_ListViewModel.Model != null)
            //{
            //    listXuatKho = k_ListXuatKho_ListViewModel.Model.getListOfInfo();
            //}

            //IList<BSP_BanHangHoa_ChiTietChild> BanHangHoaChiTietList = this.GetViewModel().Model.BSP_BanHangHoa_ChiTiets;

            //List<K_KhoInfo> ListXuatKhoRutGon = new List<K_KhoInfo>();

            //List<K_KhoInfo> ListXuatKhoCanChinhSua = new List<K_KhoInfo>();

            //if (listXuatKho != null)
            //{
            //    foreach (K_ListXuatKhoInfo ListKhoInfo in listXuatKho)
            //    {
            //        if (ListKhoInfo.HangHoaId == BanHangHoaChiTietList[e.Index].HangHoaId && ListKhoInfo.SoLuong != null)
            //        {
            //            if (this.GetViewModel().Model.Khoa != 0 && this.GetViewModel().Model.Khoa == ListKhoInfo.ChungTuThayTheId)
            //            {
            //                //do nothing . Just remove case: Editing current Xuat Kho of Ban Hang
            //            }
            //            else
            //            {
            //                if (this.GetViewModel().Model.NgayChungTu.Value.CompareTo(ListKhoInfo.NgayChungTu) < 0)
            //                {
            //                    K_KhoInfo khoInfo = new K_KhoInfo(ListKhoInfo.HangHoaId, ListKhoInfo.SoLuong, ListKhoInfo.DonGia, ListKhoInfo.NgayChungTu, ListKhoInfo.ChungTuThayTheId);
            //                    ListXuatKhoCanChinhSua.Add(khoInfo);
            //                }
            //                else
            //                {
            //                    K_KhoInfo khoInfo = new K_KhoInfo(ListKhoInfo.HangHoaId, ListKhoInfo.SoLuong, ListKhoInfo.DonGia, ListKhoInfo.NgayChungTu, ListKhoInfo.ChungTuThayTheId);
            //                    ListXuatKhoRutGon.Add(khoInfo);
            //                }
            //            }
            //        }
            //    }
            //}
            //ListXuatKhoCanChinhSua = K_KhoInfo.NhapTruocXuatTruocSort(ListXuatKhoCanChinhSua);
            //ListXuatKhoRutGon = K_KhoInfo.NhapTruocXuatTruocSort(ListXuatKhoRutGon);

            //List<K_KhoInfo> listHangTonVaNhap = new List<K_KhoInfo>();

            //if (listNhapKho != null)
            //{
            //    foreach (K_ListNhapKhoInfo item in listNhapKho)
            //    {
            //        if (item.HangHoaId == BanHangHoaChiTietList[e.Index].HangHoaId && item.SoLuong != null)
            //        {
            //            K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, item.SoLuong, item.DonGiaQĐ, item.NgayChungTu);
            //            listHangTonVaNhap.Add(khoInfo);
            //        }
            //    }
            //}

            //System.Windows.Data.CollectionViewSource k_TonKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_TonKho_ViewModelSource"];
            //K_TonKho_ViewModel k_TonKho_ViewModel = ((List<object>)k_TonKho_CollectionViewSource.Source)[0] as K_TonKho_ViewModel;


            //int countListHangTonVaNhap = listHangTonVaNhap.Count;

            //if (mode.Equals("NTXT"))
            //{
            //    if (k_TonKho_ViewModel.Model.Khoa != 0)
            //    {
            //        foreach (K_TonKho_ChiTietChild item in k_TonKho_ViewModel.Model.K_TonKho_ChiTiets)
            //        {
            //            if (item.HangHoaId == BanHangHoaChiTietList[e.Index].HangHoaId)
            //            {
            //                listHangTonVaNhap = K_KhoInfo.NhapTruocXuatTruocSort(listHangTonVaNhap);
            //                K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, item.SoLuong, item.DonGia, this.GetViewModel().Model.NgayChungTu.Value.AddMonths(-1));
            //                listHangTonVaNhap.Insert(0, khoInfo);
            //                break;
            //            }
            //        }
            //    }
            //    else
            //    {   
            //        listHangTonVaNhap = K_KhoInfo.NhapTruocXuatTruocSort(listHangTonVaNhap);     
            //    }
            //}
            
            //else if (mode.Equals("NSXT"))
            //{
            //    if (k_TonKho_ViewModel.Model.Khoa != 0)
            //    {
            //        foreach (K_TonKho_ChiTietChild item in k_TonKho_ViewModel.Model.K_TonKho_ChiTiets)
            //        {
            //            if (item.HangHoaId == BanHangHoaChiTietList[e.Index].HangHoaId)
            //            {
            //                listHangTonVaNhap = K_KhoInfo.NhapSauXuatTruocSort(listHangTonVaNhap);
            //                K_KhoInfo khoInfo = new K_KhoInfo(item.HangHoaId, item.SoLuong, item.DonGia, this.GetViewModel().Model.NgayChungTu.Value.AddMonths(-1));
            //                listHangTonVaNhap.Add(khoInfo);
            //                break;
            //            }
            //        }
            //    }
            //    else
            //    {
            //        listHangTonVaNhap = K_KhoInfo.NhapSauXuatTruocSort(listHangTonVaNhap);
            //    }
            //}

            //double SoLuongHangTonVaNhap = 0;
            //foreach(K_KhoInfo item in listHangTonVaNhap)
            //{
            //    if (this.GetViewModel().Model.NgayChungTu.Value.CompareTo(item.Time) > 0)
            //    {
            //        SoLuongHangTonVaNhap += item.SoLuong.Value;
            //    }
            //}

            //double SoLuongHangXuat = 0;
            //foreach (K_KhoInfo item in ListXuatKhoRutGon)
            //{
            //    SoLuongHangXuat += item.SoLuong.Value;
            //}

            //double TongSoLuongConLai = SoLuongHangTonVaNhap - SoLuongHangXuat;
           
            
            //if (BanHangHoaChiTietList[e.Index].SoLuong > TongSoLuongConLai)
            //{
            //    MessageBoxResult result = MessageBox.Show("Bạn đã nhập số lượng lớn hơn số lượng trong kho. Số lượng còn trong kho là : " + TongSoLuongConLai, "Thông tin tồn kho", MessageBoxButton.OK);
            //    BanHangHoaChiTietList[e.Index].SoLuong = 0;
            //}
            //else
            //{
            //    int CountHangTonVaNhap = listHangTonVaNhap.Count;
            //    foreach (K_KhoInfo XuatKhoItem in ListXuatKhoRutGon)
            //    {
            //        double? RemainSoLuong = XuatKhoItem.SoLuong;
            //        for (int i = 0; i < CountHangTonVaNhap; i++)
            //        {
            //            if (XuatKhoItem.Time.CompareTo(listHangTonVaNhap[i].Time) > 0 && listHangTonVaNhap[i].SoLuong != 0)
            //            {
            //                if (listHangTonVaNhap[i].SoLuong <= RemainSoLuong)
            //                {
            //                    RemainSoLuong -= listHangTonVaNhap[i].SoLuong;
            //                    listHangTonVaNhap[i].SoLuong = 0;

            //                }
            //                else
            //                {
            //                    listHangTonVaNhap[i].SoLuong -= RemainSoLuong;
            //                    break;
            //                }
            //            }
            //        }
            //    }

            //    double SoLuongConLai = BanHangHoaChiTietList[e.Index].SoLuong.Value;
            //    double TienVonChung = 0;
            //    for (int i = 0; i < CountHangTonVaNhap; i++)
            //    {
            //        if (listHangTonVaNhap[i].SoLuong != null && listHangTonVaNhap[i].DonGia != null && listHangTonVaNhap[i].SoLuong != 0 && listHangTonVaNhap[i].DonGia != 0)
            //        {
            //            if (listHangTonVaNhap[i].SoLuong < SoLuongConLai)
            //            {
            //                SoLuongConLai -= listHangTonVaNhap[i].SoLuong.Value;
            //                TienVonChung += (double)(listHangTonVaNhap[i].SoLuong * listHangTonVaNhap[i].DonGia);
            //                listHangTonVaNhap[i].SoLuong = 0;

            //            }
            //            else
            //            {
            //                listHangTonVaNhap[i].SoLuong -= SoLuongConLai;
            //                TienVonChung += (double)(SoLuongConLai * listHangTonVaNhap[i].DonGia);
            //                break;
            //            }
            //        }
            //    }

            //    TienVonChung = TienVonChung / BanHangHoaChiTietList[e.Index].SoLuong.Value;

            //    this.GetViewModel().Model.BSP_BanHangHoa_GiaVons[e.Index].DonGiaVonQĐ = TienVonChung;
            //    this.GetViewModel().Model.BSP_BanHangHoa_GiaVons[e.Index].DonGiaVon = TienVonChung / this.GetViewModel().Model.TyGia;

            //    foreach (K_KhoInfo item in ListXuatKhoCanChinhSua)
            //    {
            //        int count = listHangTonVaNhap.Count;
            //        double SoLuongXuatKhoCanChinhSua = item.SoLuong.Value;
            //        double TienVonDaDuocChinhSua = 0;
            //        for (int i = 0; i < count; i++)
            //        {
            //            if (listHangTonVaNhap[i].SoLuong != null && listHangTonVaNhap[i].DonGia != null && listHangTonVaNhap[i].SoLuong != 0 && listHangTonVaNhap[i].DonGia != 0)
            //            {
            //                if (listHangTonVaNhap[i].SoLuong < SoLuongXuatKhoCanChinhSua)
            //                {
            //                    SoLuongXuatKhoCanChinhSua -= listHangTonVaNhap[i].SoLuong.Value;
            //                    TienVonDaDuocChinhSua += (double)(listHangTonVaNhap[i].SoLuong * listHangTonVaNhap[i].DonGia);
            //                    listHangTonVaNhap[i].SoLuong = 0;

            //                }
            //                else
            //                {
            //                    listHangTonVaNhap[i].SoLuong -= SoLuongXuatKhoCanChinhSua;
            //                    TienVonDaDuocChinhSua += (double)(SoLuongXuatKhoCanChinhSua * listHangTonVaNhap[i].DonGia);
            //                    break;
            //                }
            //            }
            //        }
            //        if (item.SoLuong != null && item.SoLuong != 0)
            //        {
            //            TienVonDaDuocChinhSua = (double)(TienVonDaDuocChinhSua / item.SoLuong);
            //            item.DonGia = TienVonDaDuocChinhSua;
            //        }  
            //    }


            //    int CountOfList = TotalListCanChinhSua.Count;
            //    for (int i = CountOfList -1 ; i >=0; i--)
            //    {
            //        if (TotalListCanChinhSua[i].HangHoaId == TotalListCanChinhSua[e.Index].HangHoaId)
            //        {
            //            TotalListCanChinhSua.Remove(TotalListCanChinhSua[i]);
            //        }
            //    }

            //    foreach (K_KhoInfo item in ListXuatKhoCanChinhSua)
            //    {
            //        if (item.ChungTuThayTheId != null && item.ChungTuThayTheId != 0)
            //        {
                        
            //            K_KhoInfo newKhoInfo = new K_KhoInfo(item.ChungTuThayTheId,item.HangHoaId, item.DonGia);
            //            TotalListCanChinhSua.Add(newKhoInfo);
                        
            //        }
            //    }

            //}              
        }
        
        public void BSP_BanHangHoa_ViewModel_Saved(object sender, EventArgs e)
        {
            IndexOfBanHangHoaCanChinhSua = 0;
            if (TotalListCanChinhSua.Count != 0)
            {
                System.Windows.Data.CollectionViewSource BSP_BanHangHoa_CanChinhSua_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["BSP_BanHangHoa_CanChinhSua_ViewModelSource"];
                BSP_BanHangHoa_ViewModel BSP_BanHangHoa_CanChinhSua_ViewModel = new BSP_BanHangHoa_ViewModel(TotalListCanChinhSua[0].ChungTuThayTheId.Value);
                BSP_BanHangHoa_CanChinhSua_ViewModel.LoadCompleted += new EventHandler(BSP_BanHangHoa_CanChinhSua_ViewModel_LoadCompleted);
                BSP_BanHangHoa_CanChinhSua_ViewModel.Saved +=new EventHandler(BSP_BanHangHoa_CanChinhSua_ViewModel_Saved);
                BSP_BanHangHoa_CanChinhSua_CollectionViewSource.Source = new List<object> { BSP_BanHangHoa_CanChinhSua_ViewModel };
            }
        }
        
       
        public void BSP_BanHangHoa_CanChinhSua_ViewModel_LoadCompleted(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource BSP_BanHangHoa_CanChinhSua_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["BSP_BanHangHoa_CanChinhSua_ViewModelSource"];
            BSP_BanHangHoa_ViewModel BSP_BanHangHoa_CanChinhSua_ViewModel = ((List<object>)BSP_BanHangHoa_CanChinhSua_CollectionViewSource.Source)[0] as BSP_BanHangHoa_ViewModel;
            
            IList<BSP_BanHangHoa_GiaVonChild> listBanHangHoaGiaVon = BSP_BanHangHoa_CanChinhSua_ViewModel.Model.BSP_BanHangHoa_GiaVons;
            foreach (BSP_BanHangHoa_GiaVonChild item in listBanHangHoaGiaVon)
            {

                if (item.HangHoaId == TotalListCanChinhSua[IndexOfBanHangHoaCanChinhSua].HangHoaId)
                {
                    item.DonGiaVonQĐ = TotalListCanChinhSua[IndexOfBanHangHoaCanChinhSua].DonGia;
                    item.DonGiaVon = TotalListCanChinhSua[IndexOfBanHangHoaCanChinhSua].DonGia / BSP_BanHangHoa_CanChinhSua_ViewModel.Model.TyGia;

                }
            }
            BSP_BanHangHoa_CanChinhSua_ViewModel.SaveBanHangHoa();
            HangHoaIdCanChinhSua = 0;
            GiaVonCanChinhSua = 0;
        }

        public void BSP_BanHangHoa_CanChinhSua_ViewModel_Saved(object sender, EventArgs e)
        {
            IndexOfBanHangHoaCanChinhSua++;
            if (IndexOfBanHangHoaCanChinhSua < TotalListCanChinhSua.Count)
            {
                System.Windows.Data.CollectionViewSource BSP_BanHangHoa_CanChinhSua_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["BSP_BanHangHoa_CanChinhSua_ViewModelSource"];
                BSP_BanHangHoa_ViewModel BSP_BanHangHoa_CanChinhSua_ViewModel = new BSP_BanHangHoa_ViewModel(TotalListCanChinhSua[IndexOfBanHangHoaCanChinhSua].ChungTuThayTheId.Value);
                BSP_BanHangHoa_CanChinhSua_ViewModel.LoadCompleted += new EventHandler(BSP_BanHangHoa_CanChinhSua_ViewModel_LoadCompleted);
                BSP_BanHangHoa_CanChinhSua_ViewModel.Saved += new EventHandler(BSP_BanHangHoa_CanChinhSua_ViewModel_Saved);
                BSP_BanHangHoa_CanChinhSua_CollectionViewSource.Source = new List<object> { BSP_BanHangHoa_CanChinhSua_ViewModel };
            }

        }

        void BSP_BanHangHoa_ThueSuatIdChanged(object sender, ThueSuatIdChangedEventArgs e)
        {
            
            System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
            DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = ((List<object>)dm_ThueSuat_CollectionViewSource.Source)[0] as DM_ThueSuat_ListViewModel;    
            
            BSP_BanHangHoa_ThueChild ThueChild = GetViewModel().Model.BSP_BanHangHoa_Thues[e.Index];
            IList<DM_ThueSuatInfo> list = dm_ThueSuat_ListViewModel.Model.getListOfInfo();
            foreach (DM_ThueSuatInfo item in list)
            {
                if (item.Khoa == e.ThueSuatId)
                {
                    ThueChild.ThueSuatValue = item.GiaTriTinhThue;
                }
            }

           

        }
        void BSP_BanHangHoa_TinhChatHangHoaChanged(object sender, EventArgs e)
        {
            int? tinhChatHangHoaId = this.GetViewModel().Model.TinhChatHangHoaId;
            
            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = ((List<object>)dm_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            dm_HangHoa_ListViewModel.GetDM_HangHoa((int)tinhChatHangHoaId);
              
           
        }


        void BSP_BanHangHoa_EnableButtonLapPhieu(object sender, EventArgs e)
        {

            if (this.GetViewModel().Model.Khoa != 0 && this.GetViewModel().Model.DaThanhToan == true)
            {
                this.LapPhieuThuItemButton.IsEnabled = true;
                this.LapPhieuXuatItemButton.IsEnabled = true;
            }
            else if (this.GetViewModel().Model.Khoa != 0)
            {
                this.LapPhieuXuatItemButton.IsEnabled = true;
                this.LapPhieuThuItemButton.IsEnabled = false;
            }
            else
            {
                this.LapPhieuThuItemButton.IsEnabled = false;
                this.LapPhieuXuatItemButton.IsEnabled = false;
            }
        }

        void BSP_BanHangHoa_LoaiTienTeChanged(object sender, EventArgs e)
        {
            
            System.Windows.Data.CollectionViewSource dm_LoaiTienTe_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
            DM_LoaiTienTe_ListViewModel dm_LoaiTienTe_ListViewModel = ((List<object>)dm_LoaiTienTe_CollectionViewSource.Source)[0] as DM_LoaiTienTe_ListViewModel;
            IList<DM_LoaiTienTeInfo> list = dm_LoaiTienTe_ListViewModel.Model.getListOfInfo();
            foreach( DM_LoaiTienTeInfo item in list)
            {
                if (item.Khoa == GetViewModel().Model.LoaiTienId)
                {
                    GetViewModel().Model.TyGia = item.TyGiaQuyDoi;
                    break;
                }
            }
        }
       
        
        void BSP_BanHangHoa_HangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
           
            BSP_BanHangHoa_ChiTietChild ChiTietChild =  GetViewModel().Model.BSP_BanHangHoa_ChiTiets[e.Index];
            BSP_BanHangHoa_ThueChild ThueChild =  GetViewModel().Model.BSP_BanHangHoa_Thues[e.Index];
            BSP_BanHangHoa_PhuPhiChild PhuPhiChild =  GetViewModel().Model.BSP_BanHangHoa_PhuPhis[e.Index];
            BSP_BanHangHoa_GiaVonChild GiaVonChild = GetViewModel().Model.BSP_BanHangHoa_GiaVons[e.Index];
           

            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = ((List<object>)dm_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            System.Collections.Generic.IList<DM_HangHoaInfo> list = dm_HangHoa_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource dm_TKNo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiTiet_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKNo_ChiTiet_ListViewModel = ((List<object>)dm_TKNo_ChiTiet_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKNo_ChiTiet = dm_TKNo_ChiTiet_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource dm_TKCo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiTiet_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKCo_ChiTiet_ListViewModel = ((List<object>)dm_TKCo_ChiTiet_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKCo_ChiTiet = dm_TKCo_ChiTiet_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource dm_TKNo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_Thue_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKNo_Thue_ListViewModel = ((List<object>)dm_TKNo_Thue_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKNo_Thue = dm_TKNo_Thue_ListViewModel.Model.getListOfInfo();
            
           

            System.Windows.Data.CollectionViewSource dm_TKCo_GiaVon_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_GiaVon_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKCo_GiaVon_ListViewModel = ((List<object>)dm_TKCo_GiaVon_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKCo_GiaVon = dm_TKCo_GiaVon_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource dm_TKNo_GiaVon_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_GiaVon_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKNo_GiaVon_ListViewModel = ((List<object>)dm_TKNo_GiaVon_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKNo_GiaVon = dm_TKNo_GiaVon_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource dm_TKNo_PhuPhi_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_PhuPhi_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKNo_PhuPhi_ListViewModel = ((List<object>)dm_TKNo_PhuPhi_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKNo_PhuPhi = dm_TKNo_PhuPhi_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource dm_TKCo_PhuPhi_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_PhuPhi_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKCo_PhuPhi_ListViewModel = ((List<object>)dm_TKCo_PhuPhi_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKCo_PhuPhi = dm_TKCo_PhuPhi_ListViewModel.Model.getListOfInfo();

           
 
            foreach (DM_HangHoaInfo item in list)
            {
                if (item.Khoa == e.HangHoaId)
                {
                    ChiTietChild.DienGiai = item.TenViet;
                    ChiTietChild.KhoId = item.KhoNgamDinhId;
                    
                    ChiTietChild.DonGia = item.DonGiaBan;
                    ChiTietChild.TKNoId = listTKNo_ChiTiet[0].Khoa;
                    ChiTietChild.TKCoId = (int) item.TKKhoId;
                    ChiTietChild.DVTId = item.DonViTinhId;

                    ThueChild.HangHoaId = item.Khoa;
                    ThueChild.DienGiai = item.TenViet;
                    ThueChild.ThueSuatId = item.ThueSuatId;
                    ThueChild.TKNoId = listTKNo_Thue[0].Khoa;
                    ThueChild.TKCoId = ChiTietChild.TKCoId;

                    PhuPhiChild.HangHoaId = item.Khoa;
                    PhuPhiChild.DienGiai = item.TenViet;

                    GiaVonChild.HangHoaId = item.Khoa;
                    GiaVonChild.DienGiai = item.TenViet;
                    GiaVonChild.TKNoId = listTKNo_GiaVon[0].Khoa;
                    GiaVonChild.TKCoId = listTKCo_GiaVon[0].Khoa;

                    break;
                }
            }

        }

        void BSP_BanHangHoa_StateChanged(object sender, DieuKienNghiepVuEventArgs e)
        {
            /*int dknv = e.DieuKienNghiepVu;         
            if (dknv == 1 || dknv == 2)
            {
                DoFilterDKNV1And2();         

            }
            else if (dknv == 3 || dknv == 4)
            {
                DoFilterDKNV3And4();
       
            }
            else if (dknv == 5)
            {
                DoFilterDKNV5();   
       
            }
            else if (dknv == 6)
            {
                DoFilterDKNV6();  

            }
            else if (dknv == 7)
            {
                DoFilterDKNV7();

            }
            else if (dknv == 8)
            {
                DoFilterDKNV8();
            }
            */
        }

        public void DoFilterOnDefaultDKNV1()
        {
            System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiTiet_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_TaiKhoanNo_ChiTiet_ListViewModel.ListTaiKhoanChanged += new EventHandler(OnTaiKhoanNoChiTietChanged);
            dm_TaiKhoanNo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_ChiTiet_ListViewModel };

            System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiTiet_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_TaiKhoanCo_ChiTiet_ListViewModel.ListTaiKhoanChanged += new EventHandler(OnTaiKhoanCoChiTietChanged);
            dm_TaiKhoanCo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_ChiTiet_ListViewModel };

            System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_Thue_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_Thue_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_TaiKhoanNo_Thue_ListViewModel.ListTaiKhoanChanged += new EventHandler(OnTaiKhoanNoThueChanged);
            dm_TaiKhoanNo_Thue_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_Thue_ListViewModel };

            System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_Thue_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_Thue_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_TaiKhoanCo_Thue_ListViewModel.ListTaiKhoanChanged += new EventHandler(OnTaiKhoanCoThueChanged);
            dm_TaiKhoanCo_Thue_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_Thue_ListViewModel };

            System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_PhuPhi_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_PhuPhi_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_PhuPhi_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_TaiKhoanNo_PhuPhi_ListViewModel.ListTaiKhoanChanged += new EventHandler(OnTaiKhoanNoPhuPhiChanged);
            dm_TaiKhoanNo_PhuPhi_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_PhuPhi_ListViewModel };

            System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_PhuPhi_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_PhuPhi_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_PhuPhi_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_TaiKhoanCo_PhuPhi_ListViewModel.ListTaiKhoanChanged += new EventHandler(OnTaiKhoanCoPhuPhiChanged);
            dm_TaiKhoanCo_PhuPhi_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_PhuPhi_ListViewModel };

            System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_GiaVon_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_GiaVon_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_GiaVon_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_TaiKhoanNo_GiaVon_ListViewModel.ListTaiKhoanChanged += new EventHandler(OnTaiKhoanNoGiaVonChanged);
            dm_TaiKhoanNo_GiaVon_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_GiaVon_ListViewModel };

            System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_GiaVon_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_GiaVon_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_GiaVon_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_TaiKhoanCo_GiaVon_ListViewModel.ListTaiKhoanChanged += new EventHandler(OnTaiKhoanCoGiaVonChanged);
            dm_TaiKhoanCo_GiaVon_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_GiaVon_ListViewModel };
        
        }
       

        private void OnTaiKhoanNoChiTietChanged(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TKNo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiTiet_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKNo_ChiTiet_ListViewModel = ((List<object>)dm_TKNo_ChiTiet_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            
            if (dm_TKNo_ChiTiet_ListViewModel.Model != null && GetViewModel().Model != null)
            {
                System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKNo_ChiTiet = dm_TKNo_ChiTiet_ListViewModel.Model.getListOfInfo();
            
                IList<BSP_BanHangHoa_ChiTietChild> list = this.GetViewModel().Model.BSP_BanHangHoa_ChiTiets;
                foreach (BSP_BanHangHoa_ChiTietChild item in list)
                {
                    item.TKNoId = listTKNo_ChiTiet[0].Khoa;
                }
            }
        }
            

        private void OnTaiKhoanNoThueChanged(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TKNo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_Thue_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKNo_Thue_ListViewModel = ((List<object>)dm_TKNo_Thue_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            
            if (dm_TKNo_Thue_ListViewModel.Model != null && GetViewModel().Model != null)
            {
                System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKNo_Thue = dm_TKNo_Thue_ListViewModel.Model.getListOfInfo();

                IList<BSP_BanHangHoa_ThueChild> list = this.GetViewModel().Model.BSP_BanHangHoa_Thues;
                foreach (BSP_BanHangHoa_ThueChild item in list)
                {
                    item.TKNoId = listTKNo_Thue[0].Khoa;
                }
            }
        }

        private void OnTaiKhoanNoPhuPhiChanged(object sender, EventArgs e)
        {
            //System.Windows.Data.CollectionViewSource dm_TKNo_PhuPhi_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_PhuPhi_ListViewModelSource"];
            //DM_TaiKhoan_ListViewModel dm_TKNo_PhuPhi_ListViewModel = ((List<object>)dm_TKNo_PhuPhi_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            
            //if (dm_TKNo_PhuPhi_ListViewModel.Model != null && GetViewModel().Model != null)
            //{
            //    System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKNo_PhuPhi = dm_TKNo_PhuPhi_ListViewModel.Model.getListOfInfo();

            //    IList<BSP_BanHangHoa_PhuPhiChild> list = this.GetViewModel().Model.BSP_BanHangHoa_PhuPhis;
            //    foreach (BSP_BanHangHoa_PhuPhiChild item in list)
            //    {
            //        item.TkNoId = listTKNo_PhuPhi[0].Khoa;
            //    }
            //}
        }

        

        private void OnTaiKhoanNoGiaVonChanged(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TKNo_GiaVon_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_GiaVon_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKNo_GiaVon_ListViewModel = ((List<object>)dm_TKNo_GiaVon_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            
            if (dm_TKNo_GiaVon_ListViewModel.Model != null && GetViewModel().Model != null)
            {
                System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKNo_GiaVon = dm_TKNo_GiaVon_ListViewModel.Model.getListOfInfo();

                IList<BSP_BanHangHoa_GiaVonChild> list = this.GetViewModel().Model.BSP_BanHangHoa_GiaVons;
                foreach (BSP_BanHangHoa_GiaVonChild item in list)
                {
                    item.TKNoId = listTKNo_GiaVon[0].Khoa;
                }
            }
        }

     
        private void OnTaiKhoanCoChiTietChanged(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TKCo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiTiet_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKCo_ChiTiet_ListViewModel = ((List<object>)dm_TKCo_ChiTiet_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            if (dm_TKCo_ChiTiet_ListViewModel.Model != null && GetViewModel().Model != null)
            {
                System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKCo_ChiTiet = dm_TKCo_ChiTiet_ListViewModel.Model.getListOfInfo();

                IList<BSP_BanHangHoa_ChiTietChild> list = this.GetViewModel().Model.BSP_BanHangHoa_ChiTiets;
                foreach (BSP_BanHangHoa_ChiTietChild item in list)
                {
                    item.TKCoId = listTKCo_ChiTiet[0].Khoa;
                }
             }
        }

        private void OnTaiKhoanCoThueChanged(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TKCo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_Thue_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKCo_Thue_ListViewModel = ((List<object>)dm_TKCo_Thue_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            if (dm_TKCo_Thue_ListViewModel.Model != null && GetViewModel().Model != null)
            {
                System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKCo_Thue = dm_TKCo_Thue_ListViewModel.Model.getListOfInfo();

                IList<BSP_BanHangHoa_ThueChild> list = this.GetViewModel().Model.BSP_BanHangHoa_Thues;
                foreach (BSP_BanHangHoa_ThueChild item in list)
                {
                    item.TKCoId = listTKCo_Thue[0].Khoa;
                }
            }
        }

        private void OnTaiKhoanCoPhuPhiChanged(object sender, EventArgs e)
        {

            System.Windows.Data.CollectionViewSource dm_TKCo_PhuPhi_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_PhuPhi_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKCo_PhuPhi_ListViewModel = ((List<object>)dm_TKCo_PhuPhi_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            //if (dm_TKCo_PhuPhi_ListViewModel.Model != null && GetViewModel().Model != null)
            //{
            //    System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKCo_PhuPhi = dm_TKCo_PhuPhi_ListViewModel.Model.getListOfInfo();

            //    IList<BSP_BanHangHoa_PhuPhiChild> list = this.GetViewModel().Model.BSP_BanHangHoa_PhuPhis;
            //    foreach (BSP_BanHangHoa_PhuPhiChild item in list)
            //    {
            //        item.TkCoId = listTKCo_PhuPhi[0].Khoa;
            //    }
            //}
        }

        
        private void OnTaiKhoanCoGiaVonChanged(object sender, EventArgs e)
        {
              System.Windows.Data.CollectionViewSource dm_TKCo_GiaVon_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_GiaVon_ListViewModelSource"];
              DM_TaiKhoan_ListViewModel dm_TKCo_GiaVon_ListViewModel = ((List<object>)dm_TKCo_GiaVon_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
              if (dm_TKCo_GiaVon_ListViewModel.Model != null && GetViewModel().Model != null)
              {
                   System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKCo_GiaVon = dm_TKCo_GiaVon_ListViewModel.Model.getListOfInfo();

                   IList<BSP_BanHangHoa_GiaVonChild> list = this.GetViewModel().Model.BSP_BanHangHoa_GiaVons;
                   foreach (BSP_BanHangHoa_GiaVonChild item in list)
                   {
                        item.TKCoId = listTKCo_GiaVon[0].Khoa;
                   }
              }
             
        }

        
        
        private void AddThueDetailsItem()
        {
            GetViewModel().AddBSP_BanHangHoa_ThueItem();
        }

        private void AddBanHangHoaDetailsItem()
        {
            GetViewModel().AddBSP_BanHangHoa_ChiTietItem();
        }

        private void AddPhuPhiDetailsItem()
        {
            GetViewModel().AddBSP_BanHangHoa_PhuPhiItem();
        }

        private void AddGiaVonDetailsItem()
        {
            GetViewModel().AddBSP_BanHangHoa_GiaVonItem();
        }

        

        private void DeleteThueDetailsItem(int Index)
        {
            GetViewModel().DeleteBSP_BanHangHoa_ThueItem(Index);
        }

        private void DeleteBanHangHoaDetailsItem(int Index)
        {
            GetViewModel().DeleteBSP_BanHangHoa_ChiTietItem( Index);
        }

        private void DeletePhuPhiDetailsItem(int Index)
        {
            GetViewModel().DeleteBSP_BanHangHoa_PhuPhiItem(Index);
        }

        private void DeleteGiaVonDetailsItem(int Index)
        {
            GetViewModel().DeleteBSP_BanHangHoa_GiaVonItem(Index);
        }

        

        void BSP_BanHangHoa_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "BSP_BanHangHoa_ChiTiets")
            {
                this.BSP_BanHangHoa_BanHangHoaChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "BSP_BanHangHoa_Thues")
            {
                this.BSP_BanHangHoa_PhuPhiGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "BSP_BanHangHoa_PhuPhis")
            {
                this.BSP_BanHangHoa_PhuPhiGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "BSP_BanHangHoa_GiaVons")
            {
                this.BSP_BanHangHoa_GiaVonGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            
        }

       
        #endregion
        
    }
}
